package com.mingweicai.intergration.thread.pc1;

/**
 * 消费者
 */
public class WaitPerson implements Runnable{

  private Restaurant restaurant;

  public WaitPerson(Restaurant restaurant) {
    this.restaurant = restaurant;
  }

  @Override
  public void run() {

    while (!Thread.interrupted()){
      try {
        // 服务员等待厨师准备好伙食
        synchronized (this){
          while(restaurant.meal == null){
            wait();
          }
        }
        System.out.println("WaitPerson get the meal " + restaurant.meal);

        // 服务员上完菜，通知厨师后继续等待厨师准备
        synchronized (restaurant.chef){
          restaurant.meal = null;
          restaurant.chef.notifyAll();
        }
      }catch (InterruptedException e){
        e.printStackTrace();
      }
    }
  }
}
